package com.yanxinim.cn.ui.me;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.ListCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ArrayResult;
import com.yanxinim.cn.R;
import com.yanxinim.cn.adapter.MySubordinateAdapter;
import com.yanxinim.cn.bean.redpacket.MySubordinateInfo;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.util.ToastUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import okhttp3.Call;

/**
 * @ClassName MySubordinateActivity
 * @Description 我的上下级
 * @Author ChenXL
 * @Date 2021/4/20 13:35
 * @Version 1.0
 */
public class MySubordinateActivity extends BaseActivity {

    private SmartRefreshLayout mRefreshLayout;
    private SwipeRecyclerView mPullToRefreshListView;

    //我的上下级适配器
    private MySubordinateAdapter adapter;

    private List<MySubordinateInfo> items = new ArrayList<>();

    private int currentPage = 0;
    private int currentPageSize = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_activity_my_subordinate);
        initActionBar();
        initView();
        initData();
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView mTvTitle = (TextView) findViewById(R.id.tv_title_center);
        mTvTitle.setText(getString(R.string.my_subordinate));
    }

    private void initView() {
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mPullToRefreshListView = findViewById(R.id.recyclerView);
        mPullToRefreshListView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MySubordinateAdapter(MySubordinateActivity.this);

        mRefreshLayout.setOnRefreshListener(refreshLayout -> {
            currentPage = 0;
            getMySubordinateList();
        });
        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage += 1;
            getMySubordinateList();
        });

        mPullToRefreshListView.setAdapter(adapter);
    }

    private void initData() {
        currentPage = 0;
        getMySubordinateList();
    }

    private void getMySubordinateList() {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("pageIndex", String.valueOf(currentPage));
        params.put("pageSize", String.valueOf(currentPageSize));

        HttpUtils.get().url(coreManager.getConfig().MY_SUBORDINATE)
                .params(params)
                .build()
                .execute(new ListCallback<MySubordinateInfo>(MySubordinateInfo.class) {

                             @Override
                             public void onResponse(ArrayResult<MySubordinateInfo> result) {
                                 if (result.getResultCode() == 1 && result.getData() != null && result.getData().size() != 0) {
                                     items = result.getData();
                                     if (currentPage == 0) {
                                         adapter.setItems(items);
                                         if (mRefreshLayout != null) {
                                             mRefreshLayout.finishRefresh();
                                         }
                                     }else {
                                         adapter.addItems(items);
                                     }
                                     adapter.notifyDataSetChanged();
                                 }
                                 if (currentPage != 0 && mRefreshLayout != null){
                                     mRefreshLayout.finishLoadMore();
                                 }
                             }

                             @Override
                             public void onError(Call call, Exception e) {
                                 if (mRefreshLayout != null) {
                                     if (currentPage != 0) {
                                         mRefreshLayout.finishLoadMore();
                                     }else {
                                         mRefreshLayout.finishRefresh();
                                     }
                                 }
                                 ToastUtil.showNetError(mContext);
                             }
                         }
                );
    }
}
